Page({
    data: {
      balance: 0, // 余额
      amount: '', // 输入的金额
      logs: [] // 交易记录
    },
  
    onLoad() {
      // 加载本地存储的数据
      const balance = wx.getStorageSync('wallet_balance') || 0;
      const logs = wx.getStorageSync('wallet_logs') || [];
      this.setData({ balance, logs });
    },
  
    onAmountInput(event) {
      this.setData({ amount: event.detail.value });
    },
  
    // 充值
    recharge() {
      let num = parseFloat(this.data.amount);
      if (isNaN(num) || num <= 0) {
        wx.showToast({ title: '请输入有效金额', icon: 'none' });
        return;
      }
  
      let newBalance = this.data.balance + num;
      let logs = [`充值: ¥${num.toFixed(2)}`, ...this.data.logs];
  
      this.setData({ balance: newBalance, logs });
      wx.setStorageSync('wallet_balance', newBalance);
      wx.setStorageSync('wallet_logs', logs);
      wx.showToast({ title: '充值成功', icon: 'success' });
    },
  
    // 提现
    withdraw() {
      let num = parseFloat(this.data.amount);
      if (isNaN(num) || num <= 0) {
        wx.showToast({ title: '请输入有效金额', icon: 'none' });
        return;
      }
      if (num > this.data.balance) {
        wx.showToast({ title: '余额不足', icon: 'none' });
        return;
      }
  
      let newBalance = this.data.balance - num;
      let logs = [`提现: ¥${num.toFixed(2)}`, ...this.data.logs];
  
      this.setData({ balance: newBalance, logs });
      wx.setStorageSync('wallet_balance', newBalance);
      wx.setStorageSync('wallet_logs', logs);
      wx.showToast({ title: '提现成功', icon: 'success' });
    }
  });